<?php
/*
 * @Create By: Vscode
 * @Author: falcon
 * @Date: 2020-12-27 10:52:55
 */


namespace application\timer;


use application\depend\Applog;
use application\depend\Dbiov;
use application\model\PdoModel;

class FixStocks{

    public function run(){ // 每天凌晨 2点执行
        // if(date('His')>011000 && date('His')<010000) return;

        Applog::fileLog('debugTimer',['title'=>'执行修复库存任务','time'=>date('Y-m-d H:i:s')]);
        
        $this->scanProduct('product'); // 成品虚拟仓
        $this->scanProduct('product_acc'); // 配件虚拟仓
    }// func

    public function scanProduct($virtualHouse){
        $yestoday = date('Y-m-d',strtotime("-1 day")) . ' 00:00:00'; // 昨日  2020-12-25 00:00:00
        $sql = "SELECT DISTINCT(a.sku),a.id,a.available_stock from {$virtualHouse} a inner join warehouse_iodetail b on a.sku=b.sku where b.create_time >= '{$yestoday}'";
        $unitsku = PdoModel::selectData($sql,[],"昨日入库但是库存数仍然为0的SKU重新检测:{$virtualHouse}"); // 有可能最终库存仍然为0 但仍然再计算一遍
        if(!empty($unitsku) && is_array($unitsku)){
            Applog::debugLogs(['title'=>'昨天开始有出入库记录但没库存的sku','sql'=>$sql,'result'=>$unitsku,'exe-time'=>date('Ymd H:i:s'),'start-time'=>$yestoday]);
            foreach($unitsku as $k=>&$v){
                $sql = "select * from warehouse_iodetail where sku=:sku";
                $sqlData['sku'] = $v['sku'];

                $ret = PdoModel::selectData($sql,$sqlData,"查出入库记录:{$virtualHouse}");
                if(!empty($ret) && is_array($ret)){
                    $importNum = 0;
                    $exportNum = 0;
                    foreach($ret as $val){
                        if($val['operation'] == 1) $importNum += $val['amount'];
                        if($val['operation'] == 2) $exportNum += $val['amount'];
                    }// foreach

                    // 进货总数 和 出库总数 之差 != 虚拟库存的库存数  就需要记录下来； 如果相等 代表数据没问题，删除元素即可
                    $diff = $importNum - $exportNum;
                    if(intval($v['available_stock']) == $diff){
                        unset($unitsku[$k]); // 如果仍然 = 0 ，就删除; 进入下一次循环
                        continue;
                    }
                    $v['newstock'] = $diff;
                }// if

            }// foreach

            $unitsku = array_values($unitsku);

            // 更新库存
            if(!empty($unitsku)){
                foreach($unitsku as $v){
                    // if(empty($v['newstock'])) continue;
                    $sqlData['id'] = $v['id'];
                    $sqlData['sku'] = $v['sku'];
                    $sqlData['available_stock'] = $v['newstock'];
                    $sql = "update {$virtualHouse} set available_stock=:available_stock where id=:id and sku=:sku";
                    $result = PdoModel::selectData($sql,$sqlData,"更新仓库库存数:{$virtualHouse}");
                    if(false === $result) return;
                }// foreach

                Applog::debugLogs(['title'=>'更新库存记录','data'=>$unitsku,'time'=>date('Y-m-d H:i:s')]);
            }// if
            
        }// if
    }// func

}// class